package com.woniu.crowd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.crowd.common.CrowdException;
import com.woniu.crowd.infra.model.SelectApply;
import com.woniu.crowd.infra.mapper.SelectApplyMapper;
import com.woniu.crowd.infra.model.SelectLog;
import com.woniu.crowd.infra.model.SelectProduct;
import com.woniu.crowd.service.SelectApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.crowd.service.SelectLogService;
import com.woniu.crowd.service.SelectProductService;
import com.woniu.crowd.service.dto.PageSelectApplyDto;
import com.woniu.crowd.service.form.AddSelectApplyForm;
import com.woniu.crowd.service.form.CheckApplyForm;
import com.woniu.crowd.service.form.ListApplyForm;
import com.woniu.exception.LXLGException;
import com.woniu.product.client.ProductQueryClient;
import com.woniu.product.client.result.SpuResultInfo;
import com.woniu.result.Result;
import com.woniu.user.client.AddressQueryClient;
import com.woniu.user.client.result.AddressResultInfo;
import com.woniu.util.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 甄选申请表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2022-11-26
 */
@Service
public class SelectApplyServiceImpl extends ServiceImpl<SelectApplyMapper, SelectApply> implements SelectApplyService {
    @Resource
    private SelectProductService selectProductService;
    @Resource
    private AddressQueryClient addressQueryClient;

    @Resource
    private SelectLogService selectLogService;
    @Override
    @Transactional
    public void userApplySelect(AddSelectApplyForm addSelectApplyForm) {
        SelectApply selectApply = BeanUtil.copyProperties(addSelectApplyForm, SelectApply.class);
        SelectProduct byId = selectProductService.getById(selectApply.getSelectId());
        if(byId == null) {throw new LXLGException("甄选信息不存在",500);
        }
        AddressResultInfo addressResultInfo = addressQueryClient.getUserAddressById(addSelectApplyForm.getAddressId()).getData();
        if (addressResultInfo == null) throw new LXLGException(CrowdException.CROWD_ADDRESS_NON_EXISTENT);
        selectApply.setApplyStatus("REATED");
        selectApply.setApplyTime(DateUtil.parseDateLong());
        save(selectApply);
        SelectLog selectLog = new SelectLog();
        selectLog.setSelectProductId(addSelectApplyForm.getSelectId());
        selectLog.setUserId(addSelectApplyForm.getUserId());
        selectLog.setApplyId(selectApply.getApplyId());
        selectLogService.save(selectLog);

    }

    @Override
    public Result selAllApply(ListApplyForm listApplyForm) {
        QueryWrapper<SelectApply> wrapper = new QueryWrapper<>();
        if(listApplyForm.getApplyStatus()!=null){
           wrapper.eq("apply_status", listApplyForm.getApplyStatus());
        }
        IPage<SelectApply> page = page(new Page<SelectApply>(listApplyForm.getIndexPage(), listApplyForm.getPageSize()), wrapper);
        PageSelectApplyDto pageSelectApplyDto = new PageSelectApplyDto();
        pageSelectApplyDto.setTotal(page.getTotal());
        pageSelectApplyDto.setIndexPage(page.getCurrent());
        pageSelectApplyDto.setSelectApplyList(page.getRecords());
        return Result.getInstance(PageSelectApplyDto.class).setMsg("查询成功").setCode(200).setData(pageSelectApplyDto);
    }

    @Override
    public Result checkApply(CheckApplyForm checkApplyForm) {
        SelectApply apply = getById(checkApplyForm.getApplyId());
        if(apply==null){
            throw new LXLGException("申请不存在",400);
        }
        apply.setApplyStatus(checkApplyForm.getApplyStatus());
        updateById(apply);
        SelectLog selectLog = selectLogService.getOne(new QueryWrapper<SelectLog>().eq("apply_id", checkApplyForm.getApplyId()));
        selectLog.setStatus(2);
        selectLogService.updateById(selectLog);
        return Result.getInstance().setCode(200).setMsg("审核完成");
    }
}
